﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using QuanLyThuoc.DAL;
using QuanLyThuoc.BUS;
using QuanLyThuoc.Class;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmPhieuNhap : Form
    {
        //public string keyValue = "";
        public bool rAccess = true, rAdd = true, rEdit = true, rDel = true, rPrint = true;
        public int keyValue = 0;
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        bool AddMode = false;
        bool SaveMode = false;
        public static CultureInfo CulSL = new CultureInfo("");
        public static CultureInfo CulDG = new CultureInfo("");
        public static CultureInfo CulST = new CultureInfo("");
        DataSet dsPhieuNhap = new DataSet("PhieuNhapDataSet");
        

        private AutoCompleteStringCollection auIDNCC = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection auTenNCC = new AutoCompleteStringCollection();
        private AutoCompleteStringCollection auTenThuoc = new AutoCompleteStringCollection();
        private string lcTable = "dtphieunhap", lcTableCt = "dtthuocnhap", lcKeyColumn = "idpn", lcQuery = "";
        BindingSource dspn;
        BindingSource dsct;
        bool flat = false;
        public frmPhieuNhap()
        {
            InitializeComponent();
        }

       

        private void frmPhieuNhap_Load(object sender, EventArgs e)
        {
            this.HienThiDanhSach();
            this.LoadMaNCC();
            this.LoadTenNCC();
            this.LoadTenThuoc();
            this.HienThiDanhSachChiTiet();
        }
        private void HienThiDanhSach()
        {
            dspn = new BindingSource();
            dspn.DataSource = PhieuNhapBUS.SelectAll_PhieuNhap();
            grdList.AutoGenerateColumns = false;
            grdList.DataSource = dspn;
        }
        private void HienThiDanhSachChiTiet()
        {
            dsct = new BindingSource();
            dsct.DataSource = NhapThuocBUS.SelectAll_ThuocNhap();
            grdpn.AutoGenerateColumns = false;
            grdpn.DataSource = dsct;
            int i;
            for (i=0;i < grdpn.Rows.Count;i++) 
            {
                grdpn.Rows[i].HeaderCell.Value = (i + 1);
            }
        }
        private void LoadMaNCC()
        {
            this.auIDNCC.Clear();
            DataTable dtNCC = NhaCCBUS.LayNhaCCTheoMa();
            //string lcValue = "";
            foreach (DataRow row in dtNCC.Rows)
            {
               // lcValue = row["IDNCC"].ToString().PadRight(100, ' ') + "|" + row["TenNCC"].ToString().Trim();
                this.auIDNCC.Add(row["IDNCC"].ToString());
            }
            this.txtIdNCC.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtIdNCC.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtIdNCC.AutoCompleteCustomSource = this.auIDNCC;
        }
        private void LoadTenNCC()
        {
            this.auTenNCC.Clear();
            DataTable dtNCC = NhaCCBUS.LayMaNCCTheoTen();
            string lcValue = "";
            foreach (DataRow row in dtNCC.Rows)
            {

                lcValue = row["TenNCC"].ToString().PadRight(100, ' ') + "|" + row["IDNCC"].ToString().Trim();
                //this.auTenNCC.Add(row["TenNCC"].ToString());
                this.auTenNCC.Add(lcValue);
            }
            this.txtTenNCC.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtTenNCC.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtTenNCC.AutoCompleteCustomSource = this.auTenNCC;
           
        }
        private void LoadTenThuoc()
        {
            this.auTenThuoc.Clear();
            DataTable dtThuoc = ThuocBUS.LayThuocTheoTen();
            string lcValue = "";
            foreach (DataRow row in dtThuoc.Rows)
            {
                lcValue = row["TenThuoc"].ToString().PadRight(100, ' ') + "|" + row["IDThuoc"].ToString().Trim()+"|" + row["DvtMua"].ToString().PadRight(100,' ')+"|"+ row["Dvt"].ToString().PadRight(100,' ') +"|"+ row["HeSoQD"].ToString().PadRight(100,' ');
                this.auTenThuoc.Add(lcValue);
            }
            this.txtTenThuoc.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtTenThuoc.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtTenThuoc.AutoCompleteCustomSource = this.auTenThuoc;
        }
        private void ResetControl()
        {
            this.txtIdNCC.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenNCC.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtGhichu.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDThuoc.ReadOnly = !(this.AddMode||this.SaveMode);
            this.txtDVt.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtGiaNhap.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtHanSD.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtHeSo.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtNgaySX.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtthanhtien.ReadOnly = !(this.AddMode || this.SaveMode);
            this.txtSLNhap.ReadOnly = !(this.AddMode || this.SaveMode);

            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode) ? "Hủy [F4]" : "Sửa [F4]";

            this.cmdAddSave.Image = (this.AddMode || this.SaveMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdAdd.Text = (this.AddMode || this.SaveMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.grdList.Enabled = !this.plAddMode;
            this.grdpn.Enabled = !this.AddMode;
            //this.DisplayValue();

        }
        private void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {
                    this.txtIdNCC.Text = r.Cells[2].Value.ToString();
                    this.txtIdPN.Text = r.Cells[0].Value.ToString();
                    this.txtNgaynhap.Text = r.Cells[1].Value.ToString();
                    this.txtTenNCC.Text = r.Cells[3].Value.ToString();
                    this.txtGhichu.Text = r.Cells[4].Value.ToString();
                    
                }
                else
                {
                    this.txtGhichu.Text = "";
                    this.txtIdNCC.Text = "";
                    this.txtIdPN.Text = "";
                    this.txtNgaynhap.Text = "";
                    this.txtTenNCC.Text = "";
                    
                }
            }
            catch (Exception ex)
            {
               // MessageBox.Show("Lỗi:" + ex.Message);
            }
        }
        private void GanDKCT(DataGridViewRow r)
        {
            try
            {
                if (grdpn.Rows.Count != 0)
                {
                    this.txtStt.Text = r.Cells[0].Value.ToString();
                    this.txtTenThuoc.Text = r.Cells[1].Value.ToString();
                    this.txtIDThuoc.Text = r.Cells[2].Value.ToString();
                    this.txtDVtMua.Text = r.Cells[3].Value.ToString();
                    this.txtDVt.Text = r.Cells[4].Value.ToString();
                    this.txtHeSo.Text = r.Cells[5].Value.ToString();
                    this.txtSLNhap.Text = r.Cells[6].Value.ToString();
                    this.txtGiaNhap.Text = r.Cells[7].Value.ToString();
                    this.txtthanhtien.Text = r.Cells[8].Value.ToString();
                    this.txtNgaySX.Text = r.Cells[9].Value.ToString();
                    this.txtHanSD.Text = r.Cells[10].Value.ToString();
                }
                else
                {
                    this.txtStt.Text = "";
                    this.txtTenThuoc.Text = "";
                    this.txtIDThuoc.Text = "";
                    this.txtDVtMua.Text = "";
                    this.txtDVt.Text = "";
                    this.txtHeSo.Text = "";
                    this.txtSLNhap.Text = "";
                    this.txtGiaNhap.Text = "";
                    this.txtthanhtien.Text ="";
                    this.txtNgaySX.Text = "";
                    this.txtHanSD.Text = "";

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message);
            }
        }
        private void AddBlank()
        {
            this.txtGhichu.Text = "";
            this.txtIdNCC.Text = "";
            this.txtIdPN.Text = "";
            this.txtNgaynhap.Text = "";
            this.txtTenNCC.Text = "";
        }
        private void AddBlankChiTiet()
        {
            this.txtGiaNhap.Text = "";
            this.txtDVt.Text = "";
            this.txtIDThuoc.Text = "";
            this.txtHanSD.Text = "";
            this.txtthanhtien.Text = "";
            this.txtHeSo.Text = "";
            this.txtIDCT.Text = "";
            this.txtSLNhap.Text = "";
            this.txtTenThuoc.Focus();
        }
        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;


        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {

            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }

        private void Save()
        {


            int MaNCC = int.Parse(txtIdNCC.Text);
            string TenNCC = this.txtTenNCC.Text;
            DateTime NgayNhap = Convert.ToDateTime(txtNgaynhap.Text);
            string GhiChu = this.txtGhichu.Text;
            

            //DataTable dt = new DataTable();
            //dt = PhieuNhapBUS.SelectTamMax_PhieuNhap();


            //int IDPN = Convert.ToInt32(dt.Rows[0]["tam"].ToString()) + 1;

            if (string.IsNullOrEmpty(this.txtIdPN.Text.Trim()))
            {

                //string MaPN = Convert.ToDateTime(DateTime.Today).ToString("dd/MM/yyyy") + "-" + IDPN;
                 this.txtIdPN.Text = PublicClass.GetNewStringID(this.lcTable, this.lcKeyColumn, 3, "YYMMDD");
                PhieuNhapBUS.InsertPhieuNhap(txtIdPN.Text, NgayNhap, MaNCC, GhiChu);

                if (grdList.RowCount > 0)
                {
                    DataGridViewRow r = this.grdList.SelectedRows[0];
                    this.AddBlank();

                    Gan_Click();
                }
            }
            else
            {
                string MaPN = this.txtIdPN.Text;


                PhieuNhapBUS.UpdatePhieuNhaps(MaPN, NgayNhap, MaNCC, GhiChu);
            }

        }
        private void Delete()
        {
            try
            {
                string maPN = txtIdPN.Text;
               
                    if (MessageBox.Show("Bạn có muốn xóa phiếu nhập '" + txtIDCT.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (vitri == grdList.RowCount)
                        {
                            PhieuNhapBUS.DeletePhieuNhap(maPN);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                        if (vitri == 0)
                        {
                            PhieuNhapBUS.DeletePhieuNhap(maPN);
                            Gan_Click();
                            grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                            grdList.Rows[vitri + 1].Selected = true;
                            this.AddBlank();
                            Gan_Click();
                        }
                        if (vitri > 0 && vitri < grdList.RowCount)
                        {
                            PhieuNhapBUS.DeletePhieuNhap(maPN);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                    }
                
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdpn.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                this.keyValue = 0;
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();

                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        //this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }
            //else if (keyData.Equals(Keys.Control | Keys.D))
            //{
            //    if (!(this.plAddMode || this.plEditMode))
            //        this.Delete();
            //    this.ResetControl();
            //}
            //else if (keyData.Equals(Keys.Control | Keys.P))
            //{
            //    if (!(this.plAddMode || this.plEditMode))
            //        //this.Preview();
            //}
            return base.ProcessDialogKey(keyData);
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
                this.Save();
            else
                this.AddBlank();

            this.plAddMode = !this.plAddMode;
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void txtTenNCC_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtTenNCC.Text.Trim().Split('|');
            this.txtTenNCC.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIdNCC.Text = arr[1].Trim();
           
        }

        private void cmdAddSave_Click(object sender, EventArgs e)
        {
            //if (this.plAddMode || this.plEditMode)
            //{
                #region khoa
                //int nStt = int.Parse(this.txtStt.Text.Trim().PadLeft(1, '0'));

                //if (string.IsNullOrEmpty(this.txtIdmh.Text.Trim()))
                //    this.txtIDCT.Text = this.txtIDCT.Text.Trim() + nStt.ToString().PadLeft(2, '0');

                //DataRow rowN = this.chitietphieu.NewRow();
                //rowN["stt"] = nStt;
                //rowN["IDCT"] = this.txtIDCT.Text.Trim();
                //rowN["TenThuoc"] = this.txtTenThuoc.Text.Trim();
                //rowN["dvt"] = this.txtDVt.Text.Trim();
                //rowN["DVtMua"] = this.txtIDThuoc.Text.Trim();
                ////rowN["IDThuoc"] = this.tx.Text.Trim();
                //rowN["HanDate"] = this.txtHanSD.Text.Trim();
                //rowN["HeSoQD"] = this.txtHeSo.Text.Trim();

                //rowN["SL_N"] = int.Parse(this.txtSLNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
                //rowN["GiaNhap"] = int.Parse(this.txtGiaNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
                //rowN["ThanhTien"] = int.Parse(this.txtthanhtien.Text.Trim().Replace(".", "").PadLeft(1, '0'));
                
                //this.chitietphieu.Rows.Add(rowN);
                //this.chitietphieu.AcceptChanges();

                //this.grdpn.AutoGenerateColumns = false;
                //this.grdpn.DataSource = this.chitietphieu;
                //this.grdpn.Refresh();

                //this.txtStt.Text = (this.chitietphieu.Rows.Count + 1).ToString();
                #endregion
               
                    

                        DataTable dt = new DataTable();
                        dt = PhieuNhapBUS.SelectTamMax_PhieuNhap();
                        int IDPN = Convert.ToInt32(dt.Rows[0]["tam"].ToString()) + 1;
                        string MaPN;
                        if (txtIdPN.Text == "")
                        {

                            MaPN = Convert.ToDateTime(DateTime.Today).ToString("dd/MM/yyyy") + "-" + IDPN;
                        }
                        else
                        {
                            MaPN = txtIdPN.Text.Trim();
                        }
                        DataTable dtb = new DataTable();
                        dtb = PhieuNhapBUS.DemSttThuocNhap();
                        int stt = Convert.ToInt32(dtb.Rows[0]["Stt"].ToString() + 1);
                        string ctp = Convert.ToDateTime(DateTime.Today).ToString("dd/MM/yyyy") + "-" + stt;
                        int mathuoc = int.Parse(txtIDThuoc.Text);
                        string tenthuoc = txtTenThuoc.Text;
                        string dvtmua = txtDVtMua.Text;
                        string dvtinh = txtDVt.Text;
                        decimal hesoQD = Convert.ToDecimal(txtHeSo.Text);
                        int sln = int.Parse(txtSLNhap.Text);
                        int gianhap = int.Parse(txtGiaNhap.Text);
                        int tt = int.Parse(txtthanhtien.Text);
                        DateTime ngay = Convert.ToDateTime(txtNgaynhap.Text);
                        DateTime hsd = Convert.ToDateTime(txtHanSD.Text.ToString());
                        NhapThuocBUS.InsertNhapThuoc(MaPN, ctp, tenthuoc, mathuoc, dvtmua, dvtinh, hesoQD, sln, gianhap, tt, ngay, hsd);

                        this.HienThiDanhSachChiTiet();
                        this.ResetControl();
                //this.txtTenmh.Select();

                //this.DisplayValue();
           //}
        }

        private void txtTenThuoc_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtTenThuoc.Text.Trim().Split('|');
            this.txtTenThuoc.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIDThuoc.Text = arr[1].Trim();
            if (arr.Length > 2)
            {
                this.txtDVtMua.Text = arr[2].Trim();
            }
            if (arr.Length > 3)
            {
                this.txtDVt.Text = arr[3].Trim();
            }
            if (arr.Length > 4)
            {
                this.txtHeSo.Text = arr[4].Trim();
            }
        }

        private void txtSLNhap_TextChanged(object sender, EventArgs e)
        {
            decimal sln = int.Parse(this.txtSLNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal gianhap = int.Parse(this.txtGiaNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal tt = decimal.Multiply(sln, gianhap);
            this.txtSLNhap.Text = sln.ToString("n0", CulSL);
            this.txtGiaNhap.Text = gianhap.ToString("n0", CulDG);
            this.txtGiaNhap.SelectionStart = this.txtGiaNhap.Text.Length;
        }

        private void txtGiaNhap_TextChanged(object sender, EventArgs e)
        {
            decimal sln = int.Parse(this.txtSLNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal gianhap = int.Parse(this.txtGiaNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal tt = decimal.Multiply(sln, gianhap);
            this.txtSLNhap.Text = sln.ToString("n0", CulSL);
            this.txtGiaNhap.Text = gianhap.ToString("n0", CulDG);
            this.txtGiaNhap.SelectionStart = this.txtGiaNhap.Text.Length;
        }

        private void txtthanhtien_TextChanged(object sender, EventArgs e)
        {
            decimal sl = int.Parse(txtSLNhap.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal tt = int.Parse(txtthanhtien.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            decimal gianhap = sl != 0 ? decimal.Round(decimal.Divide(tt, sl), 0) : 0;

            this.txtGiaNhap.Text = gianhap.ToString("n0", CulDG);
            this.txtthanhtien.SelectionStart = this.txtthanhtien.Text.Length;
        }
        private  decimal SumInGrid(DataGridView grid, string colSum)
        {
            decimal nReturn = 0;
            try
            {
                for (int i = 0; i < grid.Rows.Count; i++)
                    if (!grid.Rows[i].IsNewRow)
                        nReturn += decimal.Parse(grid.Rows[i].Cells[colSum].Value.ToString().Trim().PadRight(1, '0'));
            }
            catch { }
            return nReturn;
        }
        private void DisplayValue()
        {
            //decimal gianhap = int.Parse(txtGiaNhap.Text.Trim());
            //decimal sl = int.Parse(txtSLNhap.Text.Trim());

            ////sl = SumInGrid(this.grdpn, "sl_n");
            ////gianhap = SumInGrid(this.grdpn, "gianhap");
            

            //decimal tt = sl * gianhap;
            //decimal.Parse(txtthanhtien.Text) = tt;
        }

        private void txtGiaNhap_Leave(object sender, EventArgs e)
        {
            int sl = int.Parse(txtSLNhap.Text);
            int gianhap = int.Parse(txtGiaNhap.Text);
            //int tt = int.Parse(this.txtthanhtien.Text);
            int tt = int.Parse(this.txtSLNhap.Text) * int.Parse(this.txtGiaNhap.Text);
            txtthanhtien.Text = tt.ToString();
        }

        private void grdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void grdpn_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            flat = true;
            try
            {
                if (this.grdpn.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdpn.SelectedRows[0];
                GanDKCT(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void grdList_Click(object sender, EventArgs e)
        {
            if (grdList.RowCount > 0)
            {
                Gan_Click();
                vitri = grdList.CurrentCell.RowIndex;
            }
        }

        private void cmdPrev_Click(object sender, EventArgs e)
        {
            if (flat == true)
            {
                dspn.MovePrevious();
            }
            else
            {
                dsct.MovePrevious();
            }
        }

        private void cmdFirst_Click(object sender, EventArgs e)
        {
            if (flat == true)
            {
                dspn.MoveFirst();
            }
            else
            {
                dsct.MoveFirst();
            }
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            if (flat == true)
            {
                dspn.MoveNext();
            }
            else
            {
                dsct.MoveNext();
            }
        }

        private void cmdLast_Click(object sender, EventArgs e)
        {
            if (flat == true)
            {
                dspn.MoveLast();
            }
            else
            {
                dsct.MoveLast();
            }
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
            {
                this.Undo();
                this.HienThiDanhSach();
            }
            else
            {

                this.Edit();
            }
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdDel_Click(object sender, EventArgs e)
        {
            this.Delete();
            HienThiDanhSach();
        }
    }
}